import sqlite3

import streamlit as st
import base64

# 处理本地图片并设置为背景
def set_local_background(image_path):
    # 读取本地图片并进行Base64编码
    with open(image_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode()

    # 注入CSS样式，使用编码后的图片作为背景
    st.markdown(
        f"""
        <style>
        .stApp {{
            background-image: url("data:image/jpeg;base64,{encoded_string}");
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: fixed;
        }}
        /* 可选：调整页面元素背景透明度，避免遮挡背景图 */
        .stBlockContainer {{
            background-color: rgba(255, 255, 255, 0.7);  /* 白色半透明背景 */
            padding: 2rem;
            border-radius: 10px;
        }}
        </style>
        """,
        unsafe_allow_html=True
    )


# 调用函数设置本地背景图（替换为你的图片路径，如"background.jpg"）
set_local_background("C:/Users/27571/Desktop/机器视觉/aisaka-taiga/apex查询/back.jpg")  # 支持jpg、png等格式





st.markdown("<h1 style='color: white; text-shadow: 2px 2px 5px black;'> 登录页面</h1>", unsafe_allow_html=True)

# 输入框
username = st.text_input("请输入用户名", key="username")
password = st.text_input("请输入密码", type="password", key="password")

# 验证函数
def yanzheng(username, password):
    conn = sqlite3.connect('taobao.db')
    cursor = conn.cursor()
    cursor.execute('SELECT * FROM users WHERE name = ? AND password = ?', (username, password))
    user = cursor.fetchone()
    conn.close()
    return user is not None



if st.button("登录"):
    if not username or not password:
        st.error(" 请填写所有字段！")
    elif len(password) < 6:
        st.error(" 密码长度不能少于6位！")
    else:
        if yanzheng(username, password):
            st.success("欢迎回来，传奇")
        else:
            st.error("登录失败！用户名或密码错误。")


st.markdown("---")
if st.button("点击创建账号"):
    st.switch_page("./createuesr.py")

